package cn.jaye.fabric.common.interceptor;

import cn.jaye.fabric.common.constants.exception.ExceptionEnum;
import cn.jaye.fabric.common.constants.Global;
import cn.jaye.fabric.common.model.ServiceException;
import cn.jaye.fabric.common.util.ResultUtil;
import cn.jaye.fabric.domain.model.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jaye
 * @create 2018-04-04 16:06
 */
public class SecurityInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(SecurityInterceptor.class);

//    @Autowired
//    private ClientService clientService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        response.setContentType("text/html; charset=utf-8");
//        PrintWriter out = response.getWriter();
        ServletOutputStream out = response.getOutputStream();
        String resultStr;

        String usernname = request.getHeader("username");
        String password = request.getHeader("password");
        String orgId = request.getHeader("orgid");
        if (StringUtils.isNotBlank(usernname) && StringUtils.isNotBlank(password) && StringUtils.isNotBlank(orgId)) {
//            LoginUserCommand loginUserCommand = new LoginUserCommand();
//            loginUserCommand.setUsername(usernname);
//            loginUserCommand.setPassword(password);
//            loginUserCommand.setOrgname(orgname);
            try {
                User user = new User(usernname, password, orgId);
                request.getSession().setAttribute(Global.SESSION_USER_KEY, user);
                return true;
//                return clientService.checkUser(loginUserCommand);
            } catch (Exception e) {
                if (e instanceof ServiceException) {
                    ServiceException ex = (ServiceException) e;
                    resultStr = ResultUtil.error(ex.getCode(), ex.getMessage());
                } else {
                    logger.error(e.getMessage());
                    e.printStackTrace();
                    resultStr = ResultUtil.error(ExceptionEnum.SYSTEM_EXCEPTION);
                }
            }
        } else {
            resultStr = ResultUtil.error(ExceptionEnum.USER_PASSWORD_MISS);
        }
        out.write(resultStr.getBytes("UTF-8"));
        out.flush();
        out.close();
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
